package com.uking.udppush;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.IBinder;

import com.uking.util.VKConstant;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UKingPlayerMessageService extends Service {
    private static String TAG = com.smalls.newvideotwo.utils.Constant.baddcoolio2tag? com.smalls.newvideotwo.utils.Constant.TAGPREFIXSTR + "KbMessageService":"KbMessageService";
    private final IBinder mIbinder = new MyBinder();
    private int port = VKConstant.DEFAULT_VOD_PORT;
    UdpHelper udphelper;

    public class Client implements Runnable {
        public void run() {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            try {
                InetAddress serverAddr = InetAddress.getByName(UdpHelper.SERVERIP);
                DatagramSocket socket = new DatagramSocket();
                byte[] buf = new byte[102];
                buf = "21000".getBytes();
                socket.send(new DatagramPacket(buf, buf.length, serverAddr, UKingPlayerMessageService.this.port));
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("client run().......");
        }
    }

    public class MyBinder extends Binder {
        public UKingPlayerMessageService getService() {
            return UKingPlayerMessageService.this;
        }
    }

    public IBinder onBind(Intent arg0) {
        return this.mIbinder;
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @SuppressLint("WifiManagerLeak")
    public void startUdpListener(int port) {
        this.port = port;
        this.udphelper = new UdpHelper(getApplicationContext(), (WifiManager) getApplication().getSystemService(getApplication().WIFI_SERVICE), port);
        new Thread(this.udphelper).start();
        super.onCreate();
    }

    public void stopUdpListner() {
        if (this.udphelper != null) {
            this.udphelper.stop();
        }
    }

    public void onDestroy() {
        super.onDestroy();
    }

    public boolean onUnbind(Intent intent) {
        stopUdpListner();
        return super.onUnbind(intent);
    }
}
